package cn.sinata.wanglin.village.activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class ApplyActiveActivity extends DialogActivity {
    @BindView(R.id.et_nickname)
    EditText et_nickname;//昵称的输入框
    @BindView(R.id.et_phone)
    EditText et_phone;//手机号的输入框
    private String   activityId;//活动ID
    @Override
    protected int setContentLayout() {
        return R.layout.activity_apply_active;
    }

    @Override
    protected void initView() {
        setCancelOnTouchOutside(false);
        Bundle mBundle=getIntent().getExtras();
        if(mBundle!=null){
            activityId  = mBundle.getString("activityId");

        }
    }
    @OnClick({R.id.tv_doapply,R.id.iv_accoutlogin_delete})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_doapply:
                //描述:报名
                String nickname= et_nickname.getText().toString();
                String phone= et_phone.getText().toString();
                if(TextUtils.isEmpty(nickname)){
                    showToast("姓名不能为空！");
                    return;
                }
                if(TextUtils.isEmpty(phone)||phone.length() != 11||!StringUtils.isPhoneNumberValid(phone)){
                    showToast("请输入正确的手机号！");
                    return;
                }
                showDialog("报名中...");
                String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
                HttpManager.applyActivity(userid,activityId,nickname,phone).subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        setResult(RESULT_OK);
                        finish();
                    }
                });
                break;
            case R.id.iv_accoutlogin_delete:
                finish();
                break;
        }
    }
}
